package com.smsssendactivity.qingyuan;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    EditText number;
    EditText content;
    Button btnSend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        number= (EditText) findViewById(R.id.number);
        content= (EditText) findViewById(R.id.content);
        btnSend= (Button) findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!validate())
                    return;
                    SmsManager.getDefault().sendTextMessage(number.getText().toString().trim(),
                            null,content.getText().toString().trim(),null,null);
                number.setText("");
                content.setText("");
                Toast.makeText(MainActivity.this,"短信发送成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
    private boolean validate() {
        String mobile=number.getText().toString();
        String content01=content.getText().toString();
        if (mobile.equals("")){
            Toast.makeText(MainActivity.this,"手机号码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }else  if (!checkMobile(mobile)){
            Toast.makeText(this,"您输入的电话号码不正确请重新输入！",Toast.LENGTH_SHORT).show();
            return false;
        }else if(content.equals("")){
                         Toast toast=Toast.makeText(this, "短信内容不能为空请重新输入！",Toast.LENGTH_LONG);
                        toast.show();
                        return false;
                     }
                 else{
                       return true;
                     }
    }
    /*
     26:      * 中国移动134.135.136.137.138.139.150.151.152.157.158.159.187.188 ,147(数据卡不验证)
     27:             中国联通130.131.132.155.156.185.186
     28:             中国电信133.153.180.189
     29:        CDMA   133,153
     30:            手机号码验证 适合目前所有的手机
     31:      */
    public boolean checkMobile(String mobile) {
        String regex="^1(3[0-9]|5[012356789]|8[0789])\\d{8}$";
                 Pattern p   =   Pattern.compile(regex);
                 Matcher m   =   p.matcher(mobile);
                 return m.find();
    }
}
